package ysq.xyg.mob.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.mob.dao.MobAdvertDao;
import ysq.xyg.mob.model.po.MobAdvert;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.mob.model.vo.MobAdvertVo;
import ysq.xyg.mob.service.MobAdvertService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 移动端模块-广告service实现
 *
 * @author shengQiang yu
 * @date 2019-10-24 17:09:58
 */
@Service("mobAdvertService")
public class MobAdvertServiceImpl extends BaseServiceImpl<MobAdvert> implements MobAdvertService {

    @Resource
    private MobAdvertDao mobAdvertDao;

    private MobAdvert mobAdvert;

    @Autowired
    public void setBaseDao(MobAdvertDao mobAdvertDao) {
        this.baseDao = mobAdvertDao;
    }


    /**
     * 分页查询
     *
     * @param mobAdvertVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:58
     */
    public ReturnEntity page(MobAdvertVo mobAdvertVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("  sort desc,update_time desc");
        List<MobAdvert> list = acceptList(mobAdvertVo);
        PageInfo<MobAdvert> page=new PageInfo<MobAdvert>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param mobAdvertVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:58
     */
    public ReturnEntity addOrUpdate(MobAdvertVo mobAdvertVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(mobAdvertVo.getId())){
            mobAdvert=new MobAdvert(currentUser);
            isUpate=false;
        }else{
            mobAdvert=selectByPrimaryKey(mobAdvertVo.getId());
            if(ObjectUtil.isNull(mobAdvert)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            mobAdvert.setUpdateTime(new Date());
            mobAdvert.setUpdateUser(currentUser.getId());
        }
        mobAdvert.setAdvertFlag(mobAdvertVo.getAdvertFlag());
        mobAdvert.setImage(mobAdvertVo.getImage());
        mobAdvert.setUrl(mobAdvertVo.getUrl());
        mobAdvert.setDescription(mobAdvertVo.getDescription());
        mobAdvert.setSort(mobAdvertVo.getSort());
        mobAdvert.setShowType(mobAdvertVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(mobAdvert);
        }else{
            num=insert(mobAdvert);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:58
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        MobAdvert mobAdvert=selectByPrimaryKey(id);
        if(mobAdvert!=null){
            mobAdvert.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            mobAdvert.setUpdateTime(new Date());
            mobAdvert.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(mobAdvert);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param mobAdvertVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-24 17:09:58
     */
    public List<MobAdvert> acceptList(MobAdvertVo mobAdvertVo){
        Example example = new Example(MobAdvert.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(mobAdvertVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",mobAdvertVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(mobAdvertVo.getShowType())){
            criteria.andEqualTo("showType",mobAdvertVo.getShowType());
        }
        example.setOrderByClause("  sort desc,update_time desc");
        return mobAdvertDao.selectByExample(example);
    }
}
